home *** CD-ROM | disk | FTP | other *** search
/ Netware Super Library / Netware Super Library.iso / mis_util / del / del_.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1990-02-10  |  5.9 KB  |  180 lines

  1. program Del_ (Input, Output);
  2. uses Dos;
  3.  
  4. {
  5. Title   : DEL-.PAS  (Deletes ALL files but specified one(s) in a directory)
  6. LastEdit: Feb 10, 1990
  7. Author  : Gianfranco "Frankie" Lanzilli - CoSysOp OMNIANET BBS - FidoNet 2:335/304.2 - Roma
  8. System  : Borland Turbo Pascal 5.5 (Ms-Dos environment)
  9. }
  10.  
  11. type ListPtr  = ^FileList;
  12.      FileList = record
  13.                   FileName : string[12];
  14.                   Next     : ListPtr
  15.                 end;
  16.  
  17. var C             : char;
  18.     FileSpecs     : PathStr;
  19.     Prompt        : boolean;
  20.     Dir           : DirStr;
  21.     Name          : NameStr;
  22.     Ext           : ExtStr;
  23.     List, Head, D : ListPtr;
  24.     Info          : SearchRec;
  25.     Error         : integer;
  26.     Counter       : word;
  27.     F             : file;
  28.  
  29. function ReadKey : char;
  30.   var Regs : registers;
  31.   begin  {ReadKey}
  32.     Regs.AH := $08;
  33.     MsDos(Regs);
  34.     ReadKey := Chr(Regs.AL)
  35.   end;  {ReadKey}
  36.  
  37. function UpCaseStr (S : string) : string;
  38.   var K : byte;
  39.       T : string;
  40.   begin  {UpCaseStr}
  41.     T := '';
  42.     for K := 0 to Length(S) do
  43.       T[K] := UpCase(S[K]);
  44.     UpCaseStr := T
  45.   end;  {UpCaseStr}
  46.  
  47. begin  {MAIN}
  48.   C := '*';
  49.   FileSpecs := '';
  50.   Prompt := false;
  51.   if (ParamCount >= 1) then
  52.     begin
  53.       if (ParamStr(1) = '?') then
  54.         begin
  55.           Writeln;
  56.           Writeln('Sintax:    DEL- [/P] <FileFileSpecs>          (Parameters order is ininfluent)');
  57.           Writeln;
  58.           Writeln('DEL- will delete ALL files BUT specified one(s) in the working directory.');
  59.           Writeln('If present, the optional parameter:  /P  will force the program to prompt');
  60.           Writeln('the user for a confirm on deletion of Read-Only, Hidden and System files,');
  61.           Writeln('everytime encountered. If:  /P  is omitted, the program won''t give theese');
  62.           Writeln('prompts and will NOT! delete Read-Only and/or Hidden and/or System files,');
  63.           Writeln('just like Dos'' DEL command does.  Dos'' WILDCARDS *, ? are welcome!');
  64.           Writeln;
  65.           Writeln('Examples:');
  66.           Writeln;
  67.           Writeln('  DEL- *.PAS            del ALL files BUT *.PAS in current dir, no prompt');
  68.           Writeln('  DEL- D:\DATA\C??OS.*  del ALL files BUT C??OS.* in D:\DATA\, no prompt');
  69.           Writeln('  DEL-                  this deletes ALL files but R-O, H, S (with confirm)');
  70.           Writeln('  DEL- *.*              this just won''t delete anything...');
  71.           Writeln('  DEL-/P *.PAS          same as the first, but prompt for R-O, H, S files');
  72.           Writeln('  DEL-/P C:\DEMO\0*.*   similar to the previous, but applied to a Path');
  73.           Writeln('  DEL-/P                same as DEL- alone, with prompts for R-O, H, S');
  74.           Writeln('  DEL-/P C:*.*          this won''t delete anything, and won''t prompt too...');
  75.           Writeln;
  76.           Writeln('DEL- is just another utility program by Gianfranco "Frankie" Lanzilli, Rome (I)');
  77.           Halt
  78.         end;
  79.       for Counter := 1 to ParamCount do
  80.         if (UpCaseStr(ParamStr(Counter)) = '/P') then
  81.           Prompt := true
  82.         else
  83.           if (FileSpecs = '') then
  84.             FileSpecs := UpCaseStr(ParamStr(Counter))
  85.           else
  86.             begin
  87.               Writeln('Invalid parameter(s) given.');
  88.               Halt
  89.             end
  90.     end;
  91.   if (FileSpecs = '') then
  92.     begin
  93.       Write('Do you mean that should I delete ALL files in current dir  <Y/N> ? ');
  94.       repeat
  95.         C := UpCase(ReadKey)
  96.       until ((C = 'N') or (C = 'Y'));
  97.       Writeln(C);
  98.       if (C = 'N') then
  99.         Halt
  100.     end;
  101.   FSplit(FileSpecs,Dir,Name,Ext);
  102.   New(List);
  103.   Head := List;
  104.   List^.Next := nil;
  105.   if (C <> 'Y') then
  106.     begin
  107.       FindFirst(FileSpecs,$27,Info);
  108.       Error := DosError;
  109.       if ((Error = 3) and (FileSpecs <> '')) then
  110.         begin
  111.           Writeln('Incorrect PathName given.');
  112.           Halt
  113.         end;
  114.       if (Error = 18) then
  115.         begin
  116.           Writeln('Specified file(s) not found.');
  117.           Halt
  118.         end;
  119.       while (Error = 0) do
  120.         begin
  121.           New(List^.Next);
  122.           List := List^.Next;
  123.           List^.FileName := Info.Name;
  124.           List^.Next := nil;
  125.           FindNext(Info);
  126.           Error := DosError
  127.         end
  128.     end;
  129.   Counter := 0;
  130.   FindFirst(Dir+'*.*',$27,Info);
  131.   while (DosError = 0) do
  132.     begin
  133.       List := Head;
  134.       while ((List^.Next <> nil) and (List^.Next^.FileName <> Info.Name)) do
  135.         List := List^.Next;
  136.       if (List^.Next <> nil) then
  137.         begin
  138.           D := List^.Next;
  139.           List^.Next := List^.Next^.Next;
  140.           Dispose(D)
  141.         end
  142.       else
  143.         if ((Info.Attr <> $20) and (Info.Attr <> $00)) then
  144.           begin
  145.             if Prompt then
  146.               begin
  147.                 Write(Dir,Info.Name,' is marked');
  148.                 if ((Info.Attr and 1) <> 0) then
  149.                   Write(' READ-ONLY');
  150.                 if ((Info.Attr and 2) <> 0) then
  151.                   Write(' HIDDEN');
  152.                 if ((Info.Attr and 4) <> 0) then
  153.                   Write(' SYSTEM');
  154.                 Write('. Delete  <Y/N> ? ');
  155.                 repeat
  156.                   C := UpCase(ReadKey)
  157.                 until ((C = 'N') or (C = 'Y'));
  158.                 Writeln(C);
  159.                 if (C = 'Y') then
  160.                   begin
  161.                     Assign(F,Dir+Info.Name);
  162.                     SetFAttr(F,$20);
  163.                     Writeln('Deleting:  ',Dir,Info.Name);
  164.                     Erase(F);
  165.                     Inc(Counter)
  166.                   end
  167.               end
  168.           end
  169.         else
  170.           begin
  171.             Assign(F,Dir+Info.Name);
  172.             Writeln('Deleting:  ',Dir,Info.Name);
  173.             Erase(F);
  174.             Inc(Counter)
  175.           end;
  176.       FindNext(Info)
  177.     end;
  178.   Writeln;
  179.   Writeln('   ',Counter,' file(s) deleted.')
  180. end.  {MAIN}